package com.junxonline.instant.admin.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.junxonline.instant.dao.entity.model.device.Group;
import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.device.GroupVO;
import com.junxonline.instant.admin.service.config.OrganizationService;
import com.junxonline.instant.admin.service.device.GroupService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * 终端分组管理Controller
 *
 * @author JunX
 * @date 2021-10-08
 */
@Log4j2
@RestController
@RequestMapping(value = "/device/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @Autowired
    private OrganizationService organizationService;

    /**
     * 查询组织机构树（TreeSelect）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:group:query")
    @OperationLog(label = "终端分组管理-查询组织机构树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryOrganizationTree")
    public CommonResult queryOrganizationTree() {
        return CommonResult.success(organizationService.queryOrganizationTree(new QueryWrapper<>()));
    }

    /**
     * 查询上级分组树（TreeSelect）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:group:query")
    @OperationLog(label = "菜单管理-查询上级分组树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryParentTree")
    public CommonResult queryParentTree(@RequestBody(required = false) GroupVO vo) {
        if (vo.getOrganizationId() == null) {
            return CommonResult.success(Collections.emptyList());
        }
        return CommonResult.success(groupService.queryParentTree(vo));
    }

    /**
     * 查询终端分组列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:group:query")
    @OperationLog(label = "终端分组管理-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) CommonReqPage<GroupVO> vo) {
        return CommonResult.success(groupService.queryList(vo, new QueryWrapper<>()));
    }

    /**
     * 查询终端分组
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:group:query")
    @OperationLog(label = "终端分组管理-查询终端分组", type = OperationEnum.QUERY)
    @PostMapping(value = "/query")
    public CommonResult query(@RequestBody(required = false) GroupVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(groupService.query(vo.getId()));
    }

    /**
     * 新增终端分组
     *
     * @param group 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:group:add")
    @OperationLog(label = "终端分组管理-新增终端分组", type = OperationEnum.ADD)
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody(required = false) Group group) {
        if (group == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        groupService.add(group);
        return CommonResult.success();
    }

    /**
     * 编辑终端分组
     *
     * @param group 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:group:edit")
    @OperationLog(label = "终端分组管理-编辑终端分组", type = OperationEnum.EDIT)
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody(required = false) Group group) {
        if (group == null || group.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        groupService.edit(group);
        return CommonResult.success();
    }

    /**
     * 删除终端分组
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "device:group:delete")
    @OperationLog(label = "终端分组管理-删除终端分组", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        groupService.delete(idsVO.getIds());
        return CommonResult.success();
    }

}
